create or replace type body plobj as

  constructor function plobj
  return self as result
  as
  begin
    --dbms_output.put_line('[plobj] costruttore '||self.object_handle);
    self.object_handle := plobj_api.new_object;
    self.locked := 0;
    return;
  end;
  
  constructor function plobj (p_handle in number)
  return self as result
  as
  begin
    self.object_handle := p_handle;
    --dbms_output.put_line('[plobj] costruttore 2 '||self.object_handle);
    self.locked := 0;
    return;
  end;
  
  member procedure free
  as
  begin
    plobj_api.free_object(self.object_handle);
  end free;

  member procedure cp (p_from varchar2, p_to varchar2) 
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.copy_field(self.object_handle, p_from, p_to);
  end cp;

  member procedure mv (p_from varchar2, p_to varchar2) 
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.rename_field(self.object_handle, p_from, p_to);
  end mv;

  member procedure rm (p_field_name in varchar2) 
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.remove_field(self.object_handle, p_field_name);
  end rm;

  member procedure copy (p_from plobj) 
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.copy(p_from.object_handle, self.object_handle);
  end copy;
  
  member procedure put (p_field_name in varchar2, p_field_value in varchar2)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value);
  end put;
  
  member procedure put (p_field_name in varchar2, p_field_value in number)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value);
  end put;
  
  member procedure put (p_field_name in varchar2, p_field_value in date)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value);
  end put;
  
  member procedure put (p_field_name in varchar2, p_field_value in blob)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value);
  end put;
  
  member procedure put (p_field_name in varchar2, p_field_value in clob)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value);
  end put;
  
  member procedure put (p_field_name in varchar2, p_field_value in plobj)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, p_field_value.object_handle);
  end put;
  
  member function v (p_field_name in varchar2)
  return varchar2
  as
    l_val varchar2(4000);
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return l_val;
  end v;
  
  member function n (p_field_name in varchar2)
  return number
  as
    l_val number;
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return l_val;
  end n;
  
  member function d (p_field_name in varchar2)
  return date
  as
    l_val date;
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return l_val;
  end d;
  
  member function c (p_field_name in varchar2)
  return clob
  as
    l_val clob;
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return l_val;
  end c;
  
  member function b (p_field_name in varchar2)
  return blob
  as
    l_val blob;
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return l_val;
  end b;
  
  member function o (p_field_name in varchar2)
  return plobj
  as
    l_val number;
  begin
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    return plobj(l_val);
  end o;
  
  member function nvlv (p_field_name in varchar2, p_value in number)
  return number
  as
    v_val number;
  begin
    if (plobj_api.is_field_null(self.object_handle, p_field_name)) Then
      return p_value;
    else
      plobj_api.get_field(self.object_handle, p_field_name, v_val);
      return v_val;
    end if;
  end;

  member function nvlv (p_field_name in varchar2, p_value in varchar2)
  return varchar2
  as
    v_val varchar2(4000);
  begin
    if (plobj_api.is_field_null(self.object_handle, p_field_name)) Then
      return p_value;
    else
      plobj_api.get_field(self.object_handle, p_field_name, v_val);
      return v_val;
    end if;
  end;

  member function nvlv (p_field_name in varchar2, p_value in date)
  return date
  as
    v_val date;
  begin
    if (plobj_api.is_field_null(self.object_handle, p_field_name)) Then
      return p_value;
    else
      plobj_api.get_field(self.object_handle, p_field_name, v_val);
      return v_val;
    end if;
  end;

  member function nvlv (p_field_name in varchar2, p_value in clob)
  return clob
  as
    v_val clob;
  begin
    if (plobj_api.is_field_null(self.object_handle, p_field_name)) Then
      return p_value;
    else
      plobj_api.get_field(self.object_handle, p_field_name, v_val);
      return v_val;
    end if;
  end;

  member function nvlv (p_field_name in varchar2, p_value in blob)
  return blob
  as
    v_val blob;
  begin
    if (plobj_api.is_field_null(self.object_handle, p_field_name)) Then
      return p_value;
    else
      plobj_api.get_field(self.object_handle, p_field_name, v_val);
      return v_val;
    end if;
  end;

  member procedure nvls (p_field_name in varchar2, p_value in number)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, nvlv(p_field_name, p_value));
  end;

  member procedure nvls (p_field_name in varchar2, p_value in varchar2)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, nvlv(p_field_name, p_value));
  end;

  member procedure nvls (p_field_name in varchar2, p_value in date)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, nvlv(p_field_name, p_value));
  end;

  member procedure nvls (p_field_name in varchar2, p_value in clob)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, nvlv(p_field_name, p_value));
  end;

  member procedure nvls (p_field_name in varchar2, p_value in blob)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.set_field(self.object_handle, p_field_name, nvlv(p_field_name, p_value));
  end;

  member function eval (p_str in varchar2) 
  return varchar2
  as
  begin
    return plobj_api.eval(self.object_handle, p_str);
  end;
  
  member procedure nextval (p_seq in varchar2, p_field_name in varchar2)
  as
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.nextval(self.object_handle, p_seq, p_field_name);
  end;
  
  member procedure substr (p_field_name in varchar2, p_start in number, p_qty in number)
  as
    l_val varchar2(4000);
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    select substr(l_val, p_start, p_qty) into l_val from dual;
    plobj_api.set_field(self.object_handle, p_field_name, l_val);
  end;
  
  member procedure upper (p_field_name in varchar2)
  as
    l_val varchar2(4000);
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    select upper(l_val) into l_val from dual;
    plobj_api.set_field(self.object_handle, p_field_name, l_val);
  end;
  
  member procedure lower (p_field_name in varchar2)
  as
    l_val varchar2(4000);
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    select lower(l_val) into l_val from dual;
    plobj_api.set_field(self.object_handle, p_field_name, l_val);
  end;
  
  member procedure append (p_field_name in varchar2, p_str in varchar2)
  as
    l_val varchar2(4000);
  begin
    if self.locked = 1 then raise_application_error(-20003,'object locked'); end if;
    plobj_api.get_field(self.object_handle, p_field_name, l_val);
    plobj_api.set_field(self.object_handle, p_field_name, l_val||p_str);
  end;
  
end;
/
